package com.immomo.test;

import com.tinify.Tinify;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//just test code
public class Demo {
    public static void main(String[] args) throws IOException {
        Tinify.setKey("xx");
//        doCompressImage("/Users/zhangningning/IdeaProjects/untitled/src/WechatIMG665.jpg");
        System.out.println("key = " + new Demo().loadKey());
    }

    public static void doCompressImage(String originPath) {
        Tinify.setKey("xx");
        System.out.println("start compress image: " + originPath);
        String tempFilePath = originPath + "_tmp";
        File oriFile = new File(originPath);
        File desFile = new File(tempFilePath);
        if (!oriFile.exists()) {
            System.out.println("文件不存在: " + originPath);
            return;
        }
        try {
            String format = String.format("源文件大小：%.2f \n压缩后大小：%.2f \n压缩比：%.2f%%", 0f, 0f, 0f);
            System.out.println(format);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String loadKey() {
        InputStream resourceAsStream = null;
        try {
            System.out.println(System.getProperty("user.home"));
            File file = new File(System.getProperty("user.home") + "/.gradle/gradle.properties");
            if (file.isHidden()) {
                System.out.println("隐藏文件");
            }
            if (!file.exists()) {
                System.out.println("文件不存在");
                return "";
            }
            resourceAsStream = new FileInputStream(file);
            Properties prop = new Properties();
            try {
                prop.load(resourceAsStream);
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
            return prop.getProperty("idea.tinnypng.key", "");

        } catch (Exception e) {
            e.printStackTrace();
            try {
                resourceAsStream.close();
            } catch (Exception e1) {
            }
            return "";
        }
    }
}
